﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ZWZ.Web.Codes
{
    public static class IdtCodeHelper
    {
        public static bool ShowIdentyCode
        {
            get
            {
                bool result = true;
                bool.TryParse(Config.ConfigAdapter.SiteDataMgr.Groups["BaseSetting"].Settings["ShowIdentyCode"].Value, out result);
                return result;
            }
        }
        /// <summary>
        /// 验证码的Session
        /// </summary>
        public static string SessionKey { get { return "IdentifyingCode_key"; } }
        /// <summary>
        /// 取得新验证码
        /// </summary>
        /// <returns></returns>
        public static string GetNewCode()
        {
            Random ran = new Random();
            int vertifycode = ran.Next(1000, 9999);
            HttpContext.Current.Session[SessionKey] = vertifycode.ToString();
            return HttpContext.Current.Session[SessionKey].ToString();
        }
        /// <summary>
        /// 取得当前在用的验证码
        /// </summary>
        /// <returns></returns>
        public static string GetLatestCode()
        {
            return HttpContext.Current.Session[SessionKey].ToString();
        }
        /// <summary>
        /// 验证验证码是否正确
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool Check(string code)
        {
            string vertifycode = HttpContext.Current.Session[SessionKey].ToString();
            return vertifycode == code.Trim();
        }
    }
}